package cn.wzy.service.impl;


import cn.wzy.dto.RoleMenuDTO;
import cn.wzy.dto.RolePermissionDTO;
import cn.wzy.mapper.RoleMapper;
import cn.wzy.pojo.Role;
import cn.wzy.results.ResponseResult;
import cn.wzy.service.RoleService;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2022-12-08 13:53:14
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role>implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    /*
    * 角色分页
    * */
    @Override
    public ResponseResult rolePage(GoPageVo goPageVo) {
        Long stater = (goPageVo.getPageIndex()-1) * goPageVo.getPageSize();
//        分页
        List<Role> roleList = roleMapper.rolePage(goPageVo.getKeyword(),stater,goPageVo.getPageSize());
//        总数
        Long total = roleMapper.getTotal(goPageVo);
        System.out.println(roleList);
//        封装
        PageVo pageVo = new PageVo(roleList, total, goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    /*
    * 删除角色
    * */
    @Override
    @Transactional
    public ResponseResult deleteRole(Long id) {
        roleMapper.deleteRoleById(id);
        return ResponseResult.okResult();
    }

    /*
    * 保存角色
    * */
    @Override
    @Transactional
    public ResponseResult saveRole(Role role) {
//        判断是否是添加
        if (Objects.isNull(role.getId())){
//            添加
            roleMapper.addRole(role);
            return ResponseResult.okResult();
        }
//        修改
        roleMapper.updateRole(role);
        return ResponseResult.okResult();
    }

    /*
    * 修改角色权限
    * */
    @Override
    @Transactional
    public ResponseResult saveRolePermission(RolePermissionDTO rolePermissionDto) {
//        根据角色id先清除角色拥有权限
        roleMapper.deleteRolePermission(rolePermissionDto.getRoleId());
//         将role中Permission加入角色权限中
        roleMapper.addRolePermissions(rolePermissionDto.getRoleId(),rolePermissionDto.getPermissionSns());
        return ResponseResult.okResult();
    }

    /*
    * 修改角色菜单
    * */
    @Override
    @Transactional
    public ResponseResult saveRoleMenu(RoleMenuDTO roleMenuDto) {
//        根据角色id清除角色拥有菜单
        roleMapper.deleteRoleMenu(roleMenuDto.getRoleId());
//        将role中Menu加入角色菜单
        roleMapper.addRoleMenus(roleMenuDto.getRoleId(),roleMenuDto.getMenuIds());
        return ResponseResult.okResult();

    }

    /*
    * 获取所有角色
    * */
    @Override
    public ResponseResult getAllRole() {
        List<Role> roleList = roleMapper.getAllRole();
        return ResponseResult.okResult(roleList);
    }
}
